<?php
namespace App\Http\Controllers;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Input;
use App\Models\aa;


class MemberController extends Controller
{

     //会员中心页面
    public function member_center()
    {

       return view('index.member_center');

    }

     //成为会员页面
    public function dredge_member(Request $request)
    {
       $res = DB::table('user')->where('id',session('userinfo')['id'])->first();//查询登录时用户的id回显余额;

        if ($request->session()->has('userinfo')){

            return view('index.dredge_member',['res'=>$res]);

        }
            echo "<script>alert('请先登录'); location.href='index_content'</script>";

    }

    //成为会员充值
    public function dredgeadd(Request $request)
    {

      $id   = session('userinfo')['id']; //获取登录时的用户id

      $row1 = $request->input('recharge');//获取表单传过来的充值额度

      $row2 = $request->input('balance'); //获取表单传过来的会员余额

      $row1int  = intval($row1);

      $user = DB::table('user')->where('id',session('userinfo')['id'])->first();//使用模型查询单条id

      $sum  = $row1int + $row2;//将充值数额与会员余额相加

      $data = DB::table('user')
              ->where('id',$id)
              ->update(['recharge' => "$row1int", 'balance' => "$sum"]);

      if($data){
         return   ['code' => '1', 'name' => '充值成功', 'url' => '/'];
      }
         return   ['code' => '2', 'name' => '充值失败'];

    }

    //会员一键呼单
    public function indents(Request $request)
    {

       if ($request->session()->has('userinfo')) {

              return view('index.indents');

       }
              echo "<script>alert('请先登录'); location.href='index_content'</script>";

    }


    public function member(Request $request)
    {

      $res = DB::table('user')->where('id',session('userinfo')['id'])->first();//查询登录时用户的id回显余额;

       if ($request->session()->has('userinfo')){

            return view('index.member',['res'=>$res]);

        }

            echo "<script>alert('请先登录'); location.href='index_content'</script>";

    }

   public function memberadd(Request $request)
   {

      $id   = session('userinfo')['id']; //获取登录时的用户id

      $user = aa::find($id);

      $row1 = $request->input('recharge');//获取表单充值数值

      $row1int  = intval($row1);

      $row2 = $user->balance;//查询会员余额多少

      $sum  = $row1int + $row2;  //将充值的数额与数据库里的余额相加

      $time = Date("Y-m-d H:i:s");//获取当前时间戳

      $data = DB::table('user')
          ->where('id',$id)
          ->update(['recharge' => "$row1int", 'balance' => "$sum",'money_time' => "$time"]);

      if($data){

          return   ['code' => '1', 'name' => '充值成功', 'url' => '/money'];

      }

          return   ['code' => '2', 'name' => '充值失败'];

   }

    //会员充值
    public function recharge(Request $request)
    {
       $res = DB::table('user')->where('id',session('userinfo')['id'])->first();//查询登录时用户的id回显余额;

       if ($request->session()->has('userinfo')){

            return view('index.recharge',['res'=>$res]);

       }

            echo "<script>alert('请先登录'); location.href='index_content'</script>";

    }

    //会员充值功能：
    public function rechargeadd(Request $request)
    {
        $id    = session('userinfo')['id'];//获取登录时的用户id

        $total = $request->input('count');

        $row1int  = intval($total);

        $user = DB::table('user')->where('id',session('userinfo')['id'])->first();//使用模型查询单条id

        $row2 = $user->balance;

        $row1 = $user->recharge;

        $sum  = $row1int + $row2;//将充值数额与会员余额相加

        if($row1int >= 1000 && $row1int<=3000){

            $sum = $sum + 50 ; //充1000送50
            DB::table('coupons')->insert([
                     'discount'=>'15',
                     'shold'=>'100',
                     'not_before'=>date('Y-m-d'),
                     'not_after'=>date("Y-m-d",strtotime("+7 day")),
                     'type'=>'日常保洁',
                     'lid'=>session('userinfo')['id']
                    ]);

        }else if($row1int >= 3000 && $row1int<=5000){

            $sum = $sum + 200 ; //充3000送200
            DB::table('coupons')->insert([
                    ['discount'=>'15','shold'=>'100','not_before'=>date('Y-m-d'),'not_after'=>date("Y-m-d",strtotime("+7 day")),'type'=>'日常保洁','lid'=>session('userinfo')['id']],
                    ['discount'=>'60','shold'=>'300','not_before'=>date('Y-m-d'),'not_after'=>date("Y-m-d",strtotime("+7 day")),'type'=>'皮具打蜡','lid'=>session('userinfo')['id']],
                    ]);

        }else if($row1int >= 5000){

            $sum = $sum + 500 ;// 充5000送200
            DB::table('coupons')->insert([
                    ['discount'=>'15','shold'=>'100','not_before'=>date('Y-m-d'),'not_after'=>date("Y-m-d",strtotime("+7 day")),'type'=>'日常保洁','lid'=>session('userinfo')['id']],
                    ['discount'=>'60','shold'=>'300','not_before'=>date('Y-m-d'),'not_after'=>date("Y-m-d",strtotime("+7 day")),'type'=>'皮具打蜡','lid'=>session('userinfo')['id']],
                    ['discount'=>'100','shold'=>'400','not_before'=>date('Y-m-d'),'not_after'=>date("Y-m-d",strtotime("+7 day")),'type'=>'美发','lid'=>session('userinfo')['id']],
                    ]);

        }

        $data = DB::table('user')
            ->where('id',$id)
            ->update(['recharge' => "$row1int", 'balance' => "$sum"]);

        if($data){

            return  ['code' => '1', 'name' => '充值成功', 'url' => '/money'];

        }

    }

    //我的钱包页面
    public function money(Request $request)
    {

     if ($request->session()->has('userinfo')){

        $id = session('userinfo')['id']; //获取登录时的用户id

        $res = DB::table('user')->where('id',session('userinfo')['id'])->first();   //使用模型查询单条id

         //判断余额是否等于或大于1000，大于等于1000即可成为我店会员
        if($res->balance>=1000){

             DB::table('user')->where('id',$id)->update(['member'=>'1']);

         }else if($res->balance<1000){

             DB::table('user')->where('id',$id)->update(['member'=>'0']);

         }

          return view('index.money',['res' => $res]);

     }

          echo "<script>alert('请先登录'); location.href='index_content'</script>";

    }

}

?>
